﻿using RMS.BLL;
using RMS.Common;
using RMS.EF.Model;
using RMS.Manage.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMS.Manage.Web.Areas.RMS.Controllers
{
    [RMSAuthorize]
    public class MenuController : BaseController
    {
        MenuBll bll = new MenuBll();
        ProgramBll pbll = new ProgramBll();
        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            var result_p = pbll.GetAllEnable();
            if (!result_p.IsSuccess)
            {
                result_p.Data = new List<RMS_Program>();
            }
            ViewBag.Programs = result_p.Data;
            return View();
        }
        /// <summary>
        /// 列表分页查询
        /// </summary>
        /// <param name="searchValue">搜索内容</param>
        /// <param name="status">状态</param>
        /// <param name="type_m">菜单类型</param>
        /// <param name="programId">项目Id</param>
        /// <returns></returns>
        public ActionResult ListGetData(S_RMS_Menu searchModel)
        {
            var result = bll.GetBySearch(searchModel);
            return Json(result);
        }
        /// <summary>
        /// 删除和批量删除
        /// </summary>
        /// <param name="id">Id字符串","分隔</param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            var result = bll.Delete(id);
            return Json(result);
        }
        /// <summary>
        /// 编辑页
        /// </summary>
        /// <param name="id">数据Id</param>
        /// <param name="parentId">父节点Id</param>
        /// <param name="programId">项目Id</param>
        /// <returns></returns>
        public ActionResult Edit(string id, string parentId, string programId)
        {
            RMS_Menu model = new RMS_Menu()
            {
                ParentId = parentId ?? "0",
                ProgramId = programId
            };
            if (!string.IsNullOrWhiteSpace(id))
            {
                var result = bll.GetById(id);
                if (!result.IsSuccess)
                {
                    ViewBag.ErrorMsg = result.Message;
                }
                else
                {
                    model = result.Data;
                }
            }
            #region 项目选项
            var result_p = pbll.GetAllEnable();
            if (!result_p.IsSuccess)
            {
                result_p.Data = new List<RMS_Program>();
            }
            List<RMS_Program> programs = result_p.Data;
            ViewBag.Programs_Select = (from p in programs
                                       select new SelectListItem()
                                       {
                                           Value = p.Id.ToString(),
                                           Text = p.Name
                                       }).ToList(); 
            #endregion
            return PartialView(model);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model">编辑后的实体</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(RMS_Menu model)
        {
            var result = new CommonOutPutVm();
            model.LastUpdateUser = UserId;//记录操作人
            if (string.IsNullOrWhiteSpace(model.Id))
            {
                result = bll.Add(model);
            }
            else
            {
                result = bll.Updata(model);
            }
            return Json(result);
        }
        /// <summary>
        /// 获取下拉数据
        /// </summary>
        /// <param name="proguamId">项目Id</param>
        /// <returns></returns>
        public ActionResult DropDownData(string programId)
        {
            CommonOutPutVm result = new CommonOutPutVm();
            S_RMS_Menu searchModel = new S_RMS_Menu()
            {
                SearchValue = string.Empty,
                Status = E_Status.All,
                Type_M = E_Type_Menu.All,
                ProgramId = programId
            };
            result = bll.GetBySearch(searchModel);
            return Json(result);
        }
    }
}